package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.domain.Address;
import com.example.domain.User;
import com.example.result.JsonResult;
import com.example.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/address")
public class AddressController extends BaseController{
    @Autowired
    private AddressService addressService;
    //分页查询
    @PostMapping("/PageList/{currentPage}/{pageSize}")
    public JsonResult insert(@PathVariable Integer currentPage, @PathVariable Integer pageSize, HttpSession session){
        QueryWrapper<Address> wrapper=new QueryWrapper<>();
        wrapper.eq("uid",getSessionUId(session));
        IPage<Address> iPage=new Page<Address>(currentPage,pageSize);
        IPage<Address> page = addressService.page(iPage,wrapper);
        return new JsonResult(true,"",page);
    }
    //aid查询
    @GetMapping("/selectId/{aid}")
    public JsonResult selectId(@PathVariable Long aid){
        QueryWrapper<Address> wrapper=new QueryWrapper<>();
        wrapper.eq("aid",aid);
        Address address = addressService.getOne(wrapper);
        if(address==null){
            throw new RuntimeException("地址信息不存在，请刷新");
        }
        return new JsonResult(true,"操作成功",address);
    }
    //修改数据
    @PutMapping("/updateRow")
    public JsonResult updateRow(@RequestBody Address address){
        QueryWrapper<Address> wrapper=new QueryWrapper<>();
        wrapper.eq("aid",address.getAid());
        address.setAid(null);
        boolean flag = addressService.update(address, wrapper);
        if(!flag){
            throw new RuntimeException("修改失败");
        }
        return new JsonResult(true,"修改成功");
    }
    //添加数据
    @PostMapping("/addRow")
    public JsonResult addRow(@RequestBody Address address, HttpSession session){
        Long sessionUId = getSessionUId(session);
        QueryWrapper<Address> wrapper=new QueryWrapper<>();
        wrapper.eq("uid",sessionUId);
        int count = addressService.count(wrapper);
        if(count>20){
            throw new RuntimeException("一个用户最多只能有20个收货地址");
        }

        address.setUid(sessionUId);
        address.setIs_default(0);
        boolean flag = addressService.save(address);
        if(!flag){
            throw new RuntimeException("添加失败");
        }
        return new JsonResult(true,"添加成功");
    }
    //删除数据
    @DeleteMapping("/deleteRow/{aid}")
    public JsonResult deleteRow(@PathVariable Long aid){
        QueryWrapper<Address> wrapper=new QueryWrapper<>();
        wrapper.eq("aid",aid);
        boolean flag = addressService.remove(wrapper);
        if(!flag){
            throw new RuntimeException("删除失败");
        }
        return new JsonResult(true,"删除成功");
    }
    //删除数据
    @PostMapping("/delete")
    public JsonResult delete(@RequestBody List<Address> list,HttpSession session){
        for(Address address:list){
            QueryWrapper<Address> wrapper=new QueryWrapper<>();
            wrapper.eq("aid",address.getAid());
            boolean flag = addressService.remove(wrapper);
            if(!flag){
                throw new RuntimeException("删除失败");
            }
        }
        return new JsonResult(true,"删除成功");
    }
    //设为默认
    @GetMapping("/setDefault/{aid}")
    public JsonResult setDefault(@PathVariable Long aid,HttpSession session){
        //获取所有属于uid的发货信息
        Long sessionUId = getSessionUId(session);
        QueryWrapper<Address> wrapper1=new QueryWrapper<>();
        wrapper1.eq("uid",sessionUId);
        List<Address> list = addressService.list(wrapper1);
        //设置所有属于uid的发货信息为is_default：0
        for (Address address : list) {
            UpdateWrapper<Address> wrapperL=new UpdateWrapper<>();
            wrapperL.eq("aid",address.getAid());
            wrapperL.set("is_default",0);
            boolean flag = addressService.update(wrapperL);
            if(!flag){
                throw new RuntimeException("设置失败,请重试");
            }
        }
        UpdateWrapper<Address> wrapper2=new UpdateWrapper<>();
        wrapper2.eq("aid",aid);
        wrapper2.set("is_default",1);
        boolean flag = addressService.update(wrapper2);
        if(!flag){
            throw new RuntimeException("设置失败,请重试");
        }
        return new JsonResult(true,"设置成功");
    }
    @GetMapping("/getAddressList")
    public JsonResult getAddressList(){
        List<Address> list = addressService.list();
        if(list==null){
            throw new RuntimeException("获取地址信息有误");
        }
        if(list.size()==0){
            throw new RuntimeException("您还未添加地址信息");
        }
        return new JsonResult(true,"",list);
    }
}
